package com.customization.api.e9sso.cmd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.customization.commons.Console;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.workflow.publicApi.WorkflowRequestTodoPA;
import com.engine.workflow.publicApi.impl.WorkflowRequestListPAImpl;
import okhttp3.*;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.workflow.workflow.WorkflowRequestComInfo;

import java.io.IOException;
import java.util.*;

public class getToDoWorkflowRequestListCmd extends AbstractCommonCommand<Map<String,Object>> {


    public getToDoWorkflowRequestListCmd(User user, Map<String,Object> params) {
        this.user = user;
        this.params = params;

    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {


        Console.log("输入参数params:"+params.toString());
        Map<String, Object> apidatas = new HashMap<String, Object>();
        apidatas.put("status",false);
        apidatas.put("msg","未查询到相关数据");
        JSONArray json= new JSONArray();
        RecordSet rs=new RecordSet();
        String type=Util.null2String(params.get("type"));

        Map<String, String> conditionsMap=   new HashMap<>();
        if(!type.equals("")){
            String sql=" select * from  WORKFLOW_BASE where WORKFLOWNAME like '%"+type+"%'";
            rs.execute(sql);
            String ids="";
            while (rs.next()){
                ids+=","+rs.getString("id");
            }
            if(ids.startsWith(",")){
                ids=ids.replaceFirst(",","");
                conditionsMap.put("workflowIds",ids);
            }
        }
        String conditions=Util.null2String(params.get("conditions"));
        if(!conditions.equals("")){
             try {
                 JSONObject conJ = JSONObject.parseObject(conditions);
                 Set<String> set = conJ.keySet();
                 Iterator<String> it = set.iterator();
                 while (it.hasNext()) {
                     String key = it.next();
                     String val = conJ.getString(key);
                     conditionsMap.put(key, val);
                 }
             }catch (Exception e){
                 apidatas.put("status",false);
                 apidatas.put("msg","conditions应该为json格式的数据");
                 return apidatas;
             }
        }


        WorkflowRequestTodoPA workflowRequestListPA = new WorkflowRequestListPAImpl();
        List list = workflowRequestListPA.getToDoWorkflowRequestList(Util.getIntValue(Util.null2String(params.get("pageNo")),1),
                Util.getIntValue(Util.null2String(params.get("pageSize")),50), this.user, conditionsMap, true, true);
        String   jsonStr = JSONObject.toJSONString(list);
        //Console.log("开始获取待办"+jsonStr);
        try {

                json = JSONArray.parseArray(jsonStr);

                    for (int i = 0; i < json.size(); i++) {
                        JSONObject obj = json.getJSONObject(i);
                        String requestId = obj.getString("requestId");

                        try{
                            rs.execute("select  *  from workflow_requestbase where requestid='"+requestId+"'");
                            if(rs.next()){
                                String requestnamenew=rs.getString("requestnamenew");
                                String requestmark=rs.getString("requestmark");
                                if(!requestnamenew.equals("")){
                                    json.getJSONObject(i).put("requestName",requestnamenew);
                                    json.getJSONObject(i).put("requestMark",requestmark);
                                }
                            }
                        }catch (Exception e){

                        }

                        if (type.indexOf("FI110") > -1) {
                            int formid = Math.abs(obj.getJSONObject("workflowBaseInfo").getInteger("formId"));

                            String sel = " select * from formtable_main_" + formid + "_dt2 dt  where dt.mainid in (select id from  formtable_main_" + formid + "  where requestid='" + requestId + "')";
                            Console.log("sel:" + sel);
                            rs.execute(sel);
                            String[] columns = rs.getColumnName();
                            JSONArray jsonArray = new JSONArray();
                            while (rs.next()) {
                                JSONObject row = new JSONObject();
                                for (String c : columns) {
                                    row.put(c, rs.getString(c));
                                }
                                jsonArray.add(row);
                            }
                            json.getJSONObject(i).put("detailsdata", jsonArray);
                        }



                    }




        } catch (Exception e) {
            e.printStackTrace();
            Console.log(e.toString());
        }



        apidatas.put("data", json);
        apidatas.put("status",true);
        apidatas.put("msg","获取成功");
        return apidatas;
    }



}
